package cn.lg.soar.system.biz.config;

import cn.lg.soar.common.util.data.DataUtil;
import cn.lg.soar.system.api.enums.OnlineModeEnum;
import cn.lg.soar.system.api.model.AuthProps;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Lazy;

import java.time.Duration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * 服务的配置
 * @author luguoxiang
 * @date 2022/5/29
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Lazy
@Getter
@Setter
@ConfigurationProperties(value = "lg.system")
public class SystemProps  extends AuthProps {

    /**
     * 终端类型配置
     */
    private Map<Integer, String> clientType;

    /**
     * 按照终端类型设置刷新token有效期
     */
    private Map<Integer, Duration> refreshTokenClientExpire = new HashMap<>(16);

    /**
     * 刷新token默认有效期
     */
    private Duration refreshTokenExpire = Duration.ofDays(7);

    /**
     * 刷新token密钥
     */
    private String refreshTokenSecret;

    /**
     * 在线模式
     */
    private OnlineModeEnum onlineMode = OnlineModeEnum.clientType;

    /**
     * 管理员账号
     */
    private Set<String> administrators;

    /**
     * 用户类型
     */
    private Map<Integer, String> userType;

    /**
     * 禁用状态配置
     */
    private Map<Integer, String> userStatus = new HashMap<>(0);

    /**
     * 账号类型映射
     */
    private Map<Integer, String> accountBindType;

    /**
     * 超级密码（可登录任何账号的密码，开发的后门，不配置相当于关闭后门）
     */
    private String superPassword;

    /**
     * 文件上传黑名单
     */
    private Set<String> fileUploadBlacklist;

    /**
     * 文件上传白名单
     */
    private Set<String> fileUploadWhitelist;

    /**
     * 非对称加密私钥有效期
     */
    private Duration rsaExpire = Duration.ofSeconds(120);

    /**
     * 验证码有效期
     */
    private Duration captchaExpire = Duration.ofSeconds(120);

    /**
     * 业务类型白名单
     */
    private Set<String> bizTypeWhitelist = new HashSet<>();

    public Duration getRefreshTokenExpire(Integer client) {
        Duration duration = refreshTokenClientExpire.get(client);
        if (duration == null) {
            if (!clientType.containsKey(client)) {
                throw new RuntimeException("终端id不存在，请配置.yml “refreshTokenClientExpire”属性");
            }
            // 使用默认有效期
            duration = refreshTokenExpire;
            refreshTokenClientExpire.put(client, duration);
        }
        return duration;
    }

    public long getRefreshTokenExpireMillis(Integer client) {
        return getRefreshTokenExpire(client).toMillis();
    }

    public long getRefreshTokenExpireMillis() {
        return refreshTokenExpire.toMillis();
    }


    public boolean isUploadBlacklist(String suffix) {
        return !DataUtil.isEmpty(fileUploadBlacklist) && fileUploadBlacklist.contains(suffix);
    }

    public boolean isUploadWhitelist(String suffix) {
        return DataUtil.isEmpty(fileUploadWhitelist) || fileUploadWhitelist.contains(suffix);
    }

}
